package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.R;
import com.mission.schedule.activity.QDFXDetailLYActivity;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.my160920.widget.EditTextC;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Date;
import java.util.List;


public class CalenderLYDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil;
    Context context;
    WindowManager windowManager;
    EditTextC content;
    View view;
    InputMethodManager imm;
    EditText edit_tv;
    LinearLayout ll;
    String title, titleId,id, uid;

    public CalenderLYDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, String title
            , String titleId, String id, String uid) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        this.titleId = titleId;
        this.id = id;
        this.uid = uid;
        this.title = title;


        setCanceledOnTouchOutside(true);

        view = LayoutInflater.from(context).inflate(R.layout.ly_dialog, null);
        setContentView(view);

        edit_tv = view.findViewById(R.id.edit_tv);
        ll = view.findViewById(R.id.ll);

        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        imm.showSoftInput(edit_tv, 0);

        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit_tv.getText().toString().isEmpty()){
                    Toast.makeText(context,"留言内容不能为空",Toast.LENGTH_SHORT).show();
                } else {
                    ly(edit_tv.getText().toString(),0);
                }
            }
        });

        edit_tv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edit_tv.getLineCount() <= 4) {
                    ViewGroup.LayoutParams llp = ll.getLayoutParams();
                    llp.height = edit_tv.getLineCount() * 66 + 125;
                    ll.requestLayout();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void load();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


    private void ly(final String mess, final int type){
        String data = lyJsonStrinf(mess, type);
        final ProgressUtil progressUtil = new ProgressUtil();
        progressUtil.ShowProgress(context,true,true,"正在留言...");
        HttpUtils httpUtils = new HttpUtils(3000);
        RequestParams params = new RequestParams();
        params.addBodyParameter("row1", "▶︎ @"+sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERNAME, "")+" 有新的留言");
        params.addBodyParameter("row2", "#"+title+"#");
        params.addBodyParameter("data", data);
        httpUtils.send(HttpMethod.POST, URLConstants.清单留言同步主留言信息, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                callBack.load();
                progressUtil.dismiss();
                dismiss();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context,"网络异常",Toast.LENGTH_SHORT).show();
                progressUtil.dismiss();
            }
        });
    }

    String localTime = "";
    String changeTime = "";

    private String lyJsonStrinf(String mess, int type){
        JSONObject ly = new JSONObject();
        localTime =  DateUtil.formatDateTimeSs(new Date());
        changeTime = DateUtil.formatDateTimeSs(new Date());
        try {
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", -1);
            jsonObject.put("mess", mess);
            jsonObject.put("updateState", 1);
            jsonObject.put("uid", uid);
            jsonObject.put("lyUid", sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, ""));
            jsonObject.put("lyName", sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERNAME, ""));
            jsonObject.put("titleId", titleId);
            jsonObject.put("cId", id);
            jsonObject.put("localTimes", localTime);
            jsonObject.put("type", type);//0 普通留言 1 系统留言
            jsonObject.put("reamrk1", android.os.Build.MODEL.replace(" ","")+" "+android.os.Build.VERSION.RELEASE);
            jsonObject.put("lyTitleImg", sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERPHOTOPATH, ""));
            jsonObject.put("reamrk", title);
            jsonObject.put("changeTime",changeTime);
            jsonArray.put(jsonObject);
            ly.put("ly",jsonArray);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ly.toString();
    }

}
